package com.sansecy.pagesnaprecyclerview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Matrix;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.widget.ImageView;

public class SPImageView extends AppCompatImageView {
    int scaleType;
    private static final int LEFT_TOP = 1;      //左上
    private static final int CENTER_TOP = 2;
    private static final int RIGHT_TOP = 3;

    private static final int LEFT_BOTTOM = 4;
    private static final int CENTER_BOTTOM = 5;
    private static final int RIGHT_BOTTOM = 6;

    public SPImageView(Context context) {
        super(context);
        setup();
    }

    public SPImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
        setup();
    }

    public SPImageView(Context context, AttributeSet attrs,
                       int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs, defStyle);
        setup();
    }

    private void setup() {
        setScaleType(ImageView.ScaleType.MATRIX);
    }

    private void init(Context context, AttributeSet attrs, int defStyle) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.SPImageView,
                defStyle, 0);
        scaleType = array.getInt(R.styleable.SPImageView_SPScaleType, CENTER_TOP);
        array.recycle();
    }

    @Override
    protected boolean setFrame(int frameLeft, int frameTop, int frameRight, int frameBottom) {
        if (getDrawable() == null) {
            return super.setFrame(frameLeft, frameTop, frameRight, frameBottom);
        }
        float frameWidth = frameRight - frameLeft;              //控件本身的大小
        float frameHeight = frameBottom - frameTop;

        float originalImageWidth = (float) getDrawable().getIntrinsicWidth();        //原始图大小
        float originalImageHeight = (float) getDrawable().getIntrinsicHeight();

        float usedScaleFactor;
        float fitHorizontallyScaleFactor = frameWidth / originalImageWidth;
        float fitVerticallyScaleFactor = frameHeight / originalImageHeight;

        usedScaleFactor = Math.max(fitHorizontallyScaleFactor, fitVerticallyScaleFactor);   //大于1的某个数

        float newImageWidth = originalImageWidth * usedScaleFactor;
        float newImageHeight = originalImageHeight * usedScaleFactor;

        Matrix matrix = getImageMatrix();
        matrix.setScale(usedScaleFactor, usedScaleFactor, 0, 0); // 以0,0点为轴中心进行缩放

        matrix.postTranslate(getDx(frameWidth, newImageWidth),
                getDy(frameHeight, newImageHeight));   //平移到最底部，平移到最右边
        setImageMatrix(matrix);

        return super.setFrame(frameLeft, frameTop, frameRight, frameBottom);
    }

    private float getDx(float frameWidth, float newImageWidth) {
        float ret = 0;
        switch (scaleType) {
            case CENTER_TOP:
            case CENTER_BOTTOM:
                ret = (frameWidth - newImageWidth) / 2;
                break;
            case RIGHT_TOP:
            case RIGHT_BOTTOM:
                ret = (frameWidth - newImageWidth);
                break;

            default:
                break;
        }

        return ret;
    }

    private float getDy(float frameHeight, float newImageHeight) {
        float ret = 0;
        switch (scaleType) {
            case LEFT_BOTTOM:
            case CENTER_BOTTOM:
            case RIGHT_BOTTOM:
                ret = frameHeight - newImageHeight;
                break;

            default:
                break;
        }

        return ret;
    }
}
